#!/bin/bash
set -u

APP_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
RDP_FILE="$APP_ROOT/Resources/DealerDevice.rdp"
LOCAL_ROOT="$HOME/DealerDevice"
MARKER="$LOCAL_ROOT/.first-run-complete"

mkdir -p "$LOCAL_ROOT/Customers" "$LOCAL_ROOT/Forms" "$LOCAL_ROOT/Pictures"

WINDOWS_APP="/Applications/Windows App.app"

if [ ! -d "$WINDOWS_APP" ]; then
    osascript -e 'display dialog "DealerDevice Online requires Microsoft Windows App. Install Windows App from the Mac App Store, then run DealerDevice Online again." buttons {"Open Mac App Store","Cancel"} default button "Open Mac App Store"' >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        open "https://apps.apple.com/us/app/windows-app/id1295203466?mt=12"
    fi
    exit 1
fi

if [ ! -f "$MARKER" ]; then
    osascript <<'APPLESCRIPT' >/dev/null 2>&1
display dialog "DealerDevice created these local folders in your Home folder:

DealerDevice/Customers
DealerDevice/Forms
DealerDevice/Pictures

For local file access inside DealerDevice, open Windows App > Settings > General and choose the DealerDevice folder for RDP folder redirection.

This is a DealerDevice Labs build. You only need to select that folder once." buttons {"Continue"} default button "Continue"
APPLESCRIPT
    touch "$MARKER"
fi

open -a "Windows App" "$RDP_FILE"
